<?php
/**
 * Smarty shared plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Function: smarty_insert_pagination<br>
 * Purpose:  generates pages.
 * TODO:: functionality of onclick ( javascript ) type should be doublecheked.
 * @author   David Aleksanyan
 * @return string
 */
function smarty_insert_pagination($params, &$smarty)
{
	$moduleMapKey = $params['moduleKey'];
	$start = $params['start'];
	$end = $params['end'];
	$onclick = $params['onclick'];
	$container = $params['container'];
	$totalCount = $params['totalcount'];
	$countOnPage = $params['countonpage'] == 0 ? COUNT_ON_PAGE : $params['countonpage'];
	$current = $params['current']+1;

	$extraString = "";
	
	echo '<div class="pagingBox">';
	if($totalCount > $countOnPage)	echo '<span class="pagingTitle">Pages: </span> ';

	foreach ($params as $key=>$value)
	{
		if($key != 'moduleKey' && $key != 'start' && $key != 'key' && $key != 'onclick' && $key != 'container' && $key != 'totalcount' && $key != 'countonpage' && $key != 'current' && $key != 'name')
		{
			$extraString .= "/".$key."/".$value;
		}
	}
	
	$linksCount = 7;
	if($linksCount%2 == 0)
	{
		$linksCount++;
	}
	$half = ($linksCount - $linksCount%2)/2;

	
	$newPagesExist = false;
	
	$linksCountTotal = ($totalCount - ($totalCount%$countOnPage)) / $countOnPage;
	
	if($totalCount%$countOnPage != 0)
	{
		$linksCountTotal ++;
	}
	
	if($linksCountTotal <= 1 )
	{
		echo '</div>';
		return;
	}
	
	$smallerThanHalf = false;
	$from = 0;
	$to = 0;
	if( $current < $half + 1)
	{
		$smallerThanHalf = true;
		$from = 1;
	}
	else
	{
		$from = $current - $half;
	}

	if($smallerThanHalf)
	{
		if($linksCount <= $linksCountTotal)
		{
			$to = $linksCount;
		}
		else
		{
			$to = $linksCountTotal;
		}
	}
	else
	{
		if($current + 3 < $linksCountTotal)
		{
			$to = $current + 3;
			$newPagesExist = true;
		}
		else
		{
			$to = $linksCountTotal;
			$newPagesExist = false;
		}
	}

	if($newPagesExist == false)
	{
		$c = ($linksCount <= $linksCountTotal) ? $linksCount : $linksCountTotal;
		if($to - $from < $c)
		{
			$from = $to - $c + 1;
		}
	}


	if($current != 1 && $linksCountTotal > 0)
	{
		$page = $current-2;
		if(isset($onclick))
		{
			echo 'javascript:goTo("'.$moduleMapKey.'",'.$page.',"'.$container.'","'.$extraString.'")';
		}
		else
		{
			$url = '/'.$moduleMapKey.'/s/'.$page.$extraString.'/';
			echo "<a href='".$url."' class='pagingAllPages'>Previous</a> ... &nbsp";
		}

	}
//	echo "FRom - " . $from;
//	echo "<br> TO - " . $to;
	for($i = $from; $i <= $to; $i++)
	{
		if($i == $current)
		{
			echo "<span class='pagingSelectedPage'>".$i."</span>   ";
		}
		else
		{
			$page = $i-1;
			if(isset($onclick))
			{
				echo 'javascript:goTo("'.$moduleMapKey.'",'.$page.',"'.$container.'","'.$extraString.'")';
			}
			else
			{
				$url = '/'.$moduleMapKey.'/s/'.$page.$extraString.'/';
				echo "<a href='".$url."' class='pagingAllPages'>".$i."</a>&nbsp";
			}
				
		}

	}


	if( $current != $linksCountTotal && $linksCountTotal > 0)
	{
		$page = $current;
		if(isset($onclick))
		{
			echo 'javascript:goTo("'.$moduleMapKey.'",'.$page.',"'.$container.'","'.$extraString.'")';
		}
		else
		{
			$url = '/'.$moduleMapKey.'/s/'.$page.$extraString.'/';
			echo "&nbsp ... <a href='".$url."' class='pagingAllPages'>Next</a>";
		}
	}
	
	echo '</div>';
}
?>
